local class = reload("class")
local tcpS = require("tcpS")
local super = reload("super.basic")

---@class tcpLS : super.basic @tcp监听
local this = class(super)

---构造函数
function this:ctor(name, ip, port)
    super.ctor(self)
    self._name = name
    self._ip = ip
    self._port = port
    self._cid = 0
end

---设置回调函数
function this:hand()
    return function (hand)
        self._hand = hand
    end
end

---启动监听
function this:launch()
    tcpS.bind(self._name, self._ip, self._port, self)
    return self:hand()
end

---连接消息
function this:accept(ufd)
    local hand = self._hand
    hand.accept(ufd)
end

function this:disc(ufd)
    local hand = self._hand
    hand.disc(ufd)
end

---接收消息
function this:recv(ufd, mid, pack, cid)
    local hand = self._hand
    hand.recv(ufd, mid, pack, cid)
end

---发送消息
function this:send(ufd, mid, pack)
    local cid = self._cid + 1
    self._cid = cid
    tcpS.send(self._name, ufd, mid, pack, cid)
    return cid
end

---回复消息
function this:reply(ufd, mid, pack, cid)
    tcpS.send(self._name, ufd, mid, pack, cid)
end

return this
